<?php

namespace App\Service;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

/**
 * App\Service\Adseat
 *
 * @property int $id
 * @property string|null $img
 * @property string|null $title
 * @property string|null $link
 * @property int|null $sort 排序
 * @property mixed $status 1发布2不发布
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Service\Adseat whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Service\Adseat whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Service\Adseat whereImg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Service\Adseat whereLink($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Service\Adseat whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Service\Adseat whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Service\Adseat whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Service\Adseat whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Adseat extends Model
{
    //
    const DISPLAY = 1;
    const DISPLAY_NONE = 2;
    protected $table = 'adseat';
    public static function getStatusList(){
        return [
            self::DISPLAY   =>  '展示',
            self::DISPLAY_NONE  =>  '不展示'
        ];
    }

    /**
     * @param $status
     *
     * @return mixed
     */
    public function getStatusString($status){
        $statusList =  $this->getStatusList();
        return $statusList[$status];
    }


    /**
     * @param Request $request
     *
     * @return $this|\Illuminate\Database\Eloquent\Builder|static
     */
    public function getPageList(Request $request){
        $this->request = $request;
        $model = $this;
        //状态
        if(isset($this->request->status)&&$this->request->status!=''){
            $model=$model->where('status','=',$this->request->status);
        }
        $model=$model->paginate($this->request->limit)
            ->toArray();
        return $model;
    }


    public static function getDisplay($length=4){
        $res = self::where('status',self::DISPLAY)->limit(4)->get();
        if($res){
            return $res->toArray();
        }else{
            return [];
        }
    }
    /**
     * @param $value
     *
     * @return mixed
     */
    public function getStatusAttribute($value){
        $config= $this->getStatusList();
        return $config[$value];
    }

    public function draft($id = []){
        if($id == []){
            $this->where('status',self::DISPLAY)->update(['status'=>self::DISPLAY_NONE]);
        }else{
            if(is_array($id)){
                $this->whereIn('id',$id)->where('status',self::DISPLAY)->update(['status'=>self::DISPLAY_NONE]);
            }else{
                $this->where('id',$id)->where('status',self::DISPLAY)->update(['status'=>self::DISPLAY_NONE]);

            }
        }
    }

    /**
     * @param array $id
     */
    public function published($id = []){
        if($id == []){
            $this->where('status',self::DISPLAY_NONE)->update(['status'=>self::DISPLAY]);
        }else{
            if(is_array($id)){
                $this->whereIn('id',$id)->where('status',self::DISPLAY_NONE)->update(['status'=>self::DISPLAY]);
            }else{
                $this->where('id',$id)->where('status',self::DISPLAY_NONE)->update(['status'=>self::DISPLAY]);

            }
        }
    }

    public function sortCol($id = [],$sort = 50){
        if($id == []){
        }else{
            if(is_array($id)){
                $this->whereIn('id',$id)->where('sort','<>',$sort)->update(['sort'=>$sort]);
            }else{
                $this->where('id',$id)->where('sort','<>',$sort)->update(['sort'=>$sort]);

            }
        }
    }

    /**
     * @param array $id
     */
    public function del($id = []){
        if($id == []){

        }else{
            if(is_array($id)){
                $this->whereIn('id',$id)->delete();
            }else{
                $this->where('id',$id)->delete();

            }
        }
    }


    /**
     * @param $post
     */
    public function insertOrUpdate($post){
        if(isset($post['id'])){
            $old = self::find($post['id']);
            $old->title = $post['title'];
            $old->link = $post['link']?$post['link']:'';
            $old->img = isset($post['img'])?$post['img']:'';
            $old->status = isset($post['status'])?$post['status']:self::DISPLAY_NONE;
            $old->sort = $post['sort']?$post['sort']:50;
            $old->save();
        }else{
            $this->title = $post['title'];
            $this->link = $post['link'];
            $this->img = isset($post['img'])?$post['img']:'';
            $this->status = isset($post['status'])?$post['status']:self::DISPLAY_NONE;
            $this->sort = $post['sort']?$post['sort']:50;
            $this->save();
        }
    }

}
